#include "resources.h"
#include "configurations.h"

#include <sqlite3.h>



LoggerPtr resources_info::logger=Logger::getLogger("resources_info");

void resources_info::setLeaf(string leaf)
{
	this->leaf=leaf;
}

void resources_info::setFolder(string folder)
{
	this->folder=folder;
}

resources_info::resources_info(){
}


void resources_info::setId(int id){
	this->id=id;
		}


		
void folder_info::insert_resources_info(folder_info *fip)
{
	string folder_name=fip->leaf;
	sqlite3 *db;
	sqlite3_open(sqlite_db_file.c_str(), &db);		
	sqlite3_stmt *stmt;	
	const char *pzTest;
	char sql[]="insert into folders_meta_info (leaf) values (?)"; 		
	sqlite3_prepare_v2(db, sql, strlen(sql), &stmt, &pzTest);
	sqlite3_bind_text(stmt,1,folder_name.c_str(),folder_name.length(),NULL);				
  	int execute_step=sqlite3_step(stmt);
	switch(		execute_step )
	{
		case	SQLITE_ROW:
		{
			LOG4CXX_DEBUG(logger, "SQLITE_ROW");
			break;
		}
		case	SQLITE_DONE :
		{
			LOG4CXX_DEBUG(logger, "SQLITE_DONE ");
			break;
		}		
	}
	sqlite3_close(db);		
}
		
bool folder_info::find_resources_info(folder_info *fip,string folder){
	bool found=false;

	LOG4CXX_DEBUG(logger,"find_resources_info:"+folder);
	
	
	sqlite3 *db;
	sqlite3_open(sqlite_db_file.c_str(), &db);	
	char locate_resource_sql[]="select * from folders_meta_info where leaf=?"; 	
	sqlite3_stmt *stmt;	
	const char *pzTest;
	sqlite3_prepare_v2(db, locate_resource_sql, strlen(locate_resource_sql), &stmt, &pzTest);
	sqlite3_bind_text(stmt,1,folder.c_str(),folder.length(),NULL);		
  	int execute_step=sqlite3_step(stmt);
	if(execute_step==SQLITE_ROW)
  	{
  		/**
  		*folder record exists
  		*/
  		LOG4CXX_DEBUG(logger,"folder record exists[sqlite3_step]:"+folder);
		int folder_id=  sqlite3_column_int(stmt, 0);
		fip->setId(folder_id);
		
  		LOG4CXX_DEBUG(logger,folder_id  );  		
		string 	leaf_in_db=string ( 
					reinterpret_cast<const char*>(	sqlite3_column_text(stmt, 1) )
					);
		LOG4CXX_DEBUG(logger, "LEAF in DB:"+leaf_in_db);										
  		found=true;  		  		  		  		
  	}else
  	{
  		LOG4CXX_DEBUG(logger,"folder record does not exists:"+folder); 
	
  		found=false;
   	}		   	
	sqlite3_close(db);
	return found;		
}